
package com.lims.模拟测试工具;

import cn.hutool.core.util.StrUtil;
import cn.hutool.crypto.SecureUtil;
import cn.hutool.crypto.asymmetric.KeyType;
import cn.hutool.crypto.asymmetric.RSA;

/**
 * @author limaosheng
 * @create 2023-08-17 14:31
 * @desc
 */
public class RSAUtil {
    //后端的公钥
    public static final String PUBLIC_KEY = "";
    //前端的私钥
    public static final String PRIVATE_KEY = "";
    //前端的公钥
    public static final String FRONT_PUBLIC_KEY = "";
    //后端的私钥
    public static final String FRONT_PRIVATE_KEY = "";


    public static String encrypt(String str){
        if(StrUtil.isBlank(str)){
            return null;
        }
        RSA rsa = SecureUtil.rsa(FRONT_PRIVATE_KEY, PUBLIC_KEY);
        // 加密
        return rsa.encryptBase64(str, KeyType.PublicKey);
    }

    public static String decrypt(String str){
        RSA rsa = SecureUtil.rsa(FRONT_PRIVATE_KEY, PUBLIC_KEY);
        // 解密
        return rsa.decryptStr(str, KeyType.PrivateKey);
    }

    public static String encryptFront(String str){
        if(StrUtil.isBlank(str)){
            return null;
        }
        RSA rsa = SecureUtil.rsa(PRIVATE_KEY, FRONT_PUBLIC_KEY);
        // 加密
        return rsa.encryptBase64(str, KeyType.PublicKey);
    }

    public static String decryptFront(String str){
        RSA rsa = SecureUtil.rsa(PRIVATE_KEY, FRONT_PUBLIC_KEY);
        // 解密
        return rsa.decryptStr(str, KeyType.PrivateKey);
    }

    public static void main(String[] args) {
        String f = encryptFront("2222");
        System.out.println(decryptFront(f));
    }
}
